# 1/2 create b
git branch page_col #create a new branch named "page_col"
# 2/2 then switch to b
git checkout page_col
# or 1+2/2 CREATE + SWITCH BRANCHES
git checkout -b page_col Git branches
Web page construction in progress…
Branches
Create & checkout a branch
“checkout” means to change the branch you are currently working on (or switch to)
Switch to other branch
You can also use git switch other_branch which is more specific
git switch page_col
cat .git/HEAD # (confirms me I moved)Rename a (local) branch
It’s the -m parameter !
- you cannot rename a remote branch –> you delete it and re-upload it
# In currently checkedout
git branch -m better_name
# in different branch (non HEAD)
git switch master
git branch test_branch # fake one
git branch -a # it's there
git branch -m test_branch test_branch2
git branch -a # yep!Merging a branch misaligned with master
E.g. I got to Github and examine branches and it tells me that wkg_branch is like this
This branch (
wkg_branch) is 2 commits ahead of, 2 commits behindmaster.
1. Evaluate changes
Review these changes to ensure you understand what will be merged.
# check commits in wkg_branchh
git log master..wkg_branch
# 4 commits...
# qlc su wkg_branch
# pasticci su wkg_branch
# boh
# testing the branch workflow
# check commits in master
git log wkg_branch..master
# 2 commits
# split git_branch + git_collab ⑂
# removed branch new_sh2. Bring the changes from master into your branch
git checkout wkg_branch!!!!! ⛔️ in progress ⛔️!!!!
Push upstream a local branch
Check out explanation here https://www.geeksforgeeks.org/how-to-set-upstream-branch-on-git/
It is important to Setup an Upstream Branch in Git to make the workflow smooth and manage branches efficiently.
An upstream branch in Git refers to a branch that serves as a reference point for another branch. Typically, it’s used to track the remote branch, allowing developers to fetch updates, compare changes, and push their commits easily. Setting an upstream branch simplifies the process of keeping local and remote repositories in sync.
- Create local branch
- Switch to local branch
git checkout -b <new_branch> - [When the current branch i.e (‘new_branch’) has no Upstream branch set]
git push –-set-upstream origin <new_branch>command (the 1st time you push) - Thereafter
git push -u origin <new_branch>(all subsequent git push commands automatically move local branch changes up to the remote branch.)
# 1 Create local branch
# 2 switch to local branch
git checkout -b <new_branch> # or
git switch <new_branch>
# 3.a git push –set-upstream command (1st time)
git push --set-upstream origin <new_branch>
# check
git branch -a # YAY!
# 3.b git push origin (nest times )
git push -u origin <new_branch>Check which Git branches are tracking which Upstream brances
git branch -vv
# * master 789640d [origin/master] split git_branch + git_collab ⑂
# page_col c86edff [origin/page_col] added color.qmd to branch
# test_branch2 c86edff added color.qmd to branch
# up bb0035b [upstream/master] git stuff
# wkg_branch 12e1e52 qlc su wkg_branch- The main branch (master) has a tracking branch of [origin/master]
- up has a tracking branch of [upstream/master]
Rename a (remote) branch
You need to
- Publish an existing local branch on remote
git push -u origin local_branch - So you delete old one and push up a new one from local repository
Merge a git branch into master
- List All Git Branches
- Switch to Master
- Merge Branch into Master
- Push Changes (push the local changes to the remote repository so everyone working on the project can fetch the latest version.)
Since merging is a type of commit, it also requires a commit message. There are two ways to specify the commit message:
# 1. List
git branch
# 2. Switch
git checkout master
# 3. The merge creates a merge commit, bringing together
# lines of development while preserving the history of the source branch.
git merge -m "Prova di merge" page_col
# 4. Push the local changes to the remote repository
git push originSee differences b/w branches
git diff master..page_col '***.qmd'QGit Rebase
- take commits from a separat branch and replay (shift the change down to the tip of master) them at the end of another brabch
- integrate recent commits without merging
you never rebase a public branch, only a private one!
YT video on diff merge - rebase
“home base” branch to track changes on the main project
https://github.com/readme/guides/configure-git-environment
“Home base branch” is not a technical Git or GitHub term, but a phrase I use to describe a branch we’ll use to keep track of upstream repository changes, which will help us easily rebase our PRs in the future…. you’ll use it to rebase your development branches and create new working branches from it.
Basically in this example, my home base branch is named
up. Your other development branches will be based on this up branch as well. In the following set of commands, Lulliter’s main branch is named master
Run these commands to setup your up branch to track changes in the upstream project repo:
# -- Terminal
git remote add upstream git@github.com:Lulliter/nerd_help.git
git fetch upstream
# From github.com:Lulliter/nerd_help
# * [new branch] master -> upstream/master
# * [new branch] new_sh -> upstream/new_sh
# * [new branch] page_col -> upstream/page_col
git checkout -b up upstream/master
# M .gitignore
# M docs/git/git_intro.html
# M docs/search.json
# M docs/sitemap.xml
# M git/git_intro.qmd
# branch 'up' set up to track 'upstream/master'.
# Switched to a new branch 'up'— WORKFLOW
The following workflow will help you make changes, submit a new PR, and update the same PR if necessary.
Whenever you want to create a new working branch, run the following commands:
# -- Terminal
git checkout up
git pull --rebase
# Then, create and switch to your working branch.
git checkout -b wkg_branch
# make and commit changes
git add ...
git commit -m "your message"
#it’s time to push your changes to your remote fork.
git push origin wkg_branch
# Opening the pull requestOnce you’ve pushed your changes, you can use the GitHub WebUI to open the PR. Simply navigate to the main project page and GitHub will automatically suggest opening a PR from the changes that most recently got pushed to your fork.
# -- Terminal
#fetch any changes from upstream and then apply them to your up branch
git checkout up
# This means you will update your up branch to match with the latest changes in the repository where you are submitting your PR.
git pull --rebase
# switch back to your working branch
git checkout wkg_branch
# update your working branch (that contains your PR changes) to contain the latest changes from upstream (via the up branch) while preserving the changes you made on your working branch
git rebase up
# Now that your working branch is current, you can make your changes.
# To make changes to source code files you will edit the file(s), and run git add to stage # them for commit like you did before.
git commit --amend
# Re-push your changes
git push -f origin wkg_branch